<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加选课信息</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../resources/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../resources/layuiadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-header">添加选课信息</div>
      <div class="layui-card-body" style="padding: 15px;">
        <form class="layui-form" action="" lay-filter="add-studentCourseInfo">

          <div class="layui-form-item">
            <label class="layui-form-label">课程列表</label>
            <div class="layui-input-block">
              <div class="layui-col-md6">
                <select id="course">
                </select>
              </div>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">学生列表</label>
            <div class="layui-input-block">
              <div class="layui-col-md6">
                <select id="student">
                </select>
              </div>
            </div>
          </div>

          <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
              <div class="layui-footer" style="left: 0;">
                <button class="layui-btn" lay-submit="" lay-filter="add-studentCourse">立即提交</button>
              </div>
            </div>
          </div>

        </form>
      </div>
    </div>
  </div>


  <script src="../../../resources/layuiadmin/layui/layui.js"></script>
  <script src="../../../resources/js/common.js"></script>
  <script>
  layui.config({
    base: '../../../resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
    ,layer = layui.layer
    ,form = layui.form;

    form.render(null, 'add-studentCourseInfo');

    getCourseList();
    getStudentList();

    // 获取课程列表
    function getCourseList(){
      $.get('/admin/listCourseMap', function(res) {
        if (res.success){
          var tempHtml = '';
          //根据课程列表动态拼接html
          res.courseList.map(function (item, index) {
            tempHtml += '<option data-id="' + item.courseId + '">'
                    + item.courseName + ' — 授课教师：' + item.teacher.teacherName + '（教师号：'+ item.teacher.teacherId +'）' + '</option>';
          });
          $('#course').html(tempHtml);
          form.render('select');
        }
      });
    }
    // 获取学生列表
    function getStudentList(){
      $.get('/admin/listStudentMap', function(res) {
        if (res.success){
          var tempHtml = '';
          //根据学生列表动态拼接html
          res.studentList.map(function (item, index) {
            tempHtml += '<option data-id="' + item.studentId + '">'
                    + item.studentName + ' — 学号：' + item.studentId + '</option>';
          });
          $('#student').html(tempHtml);
          form.render('select');
        }
      });
    }

    var studentCourse = {};

    /* 监听提交 */
    form.on('submit(add-studentCourse)', function(){
      studentCourse.course = {
        courseId : $('#course').find('option').not(function () {
          return !this.selected;
        }).data('id')
      };
      studentCourse.student = {
        studentId : $('#student').find('option').not(function () {
          return !this.selected;
        }).data('id')
      };

      var formData = new FormData();
      formData.append('studentCourseStr',JSON.stringify(studentCourse));

      //用ajax时需要注意你的url接口、采用哪一种方式type获取，它的使用的哪种数据类型datatype
      $.ajax({
        url:'/admin/addStudentCourse',
        type:'post',
        data:formData,
        contentType : false,
        processData : false,
        success:function(res){
          if (res.success){
            layer.msg("添加选课信息成功");
          }else {
            layer.msg("添加选课信息失败" + res.errMsg);
          }
        }
      })
      return false;
    });

  });
  </script>
</body>
</html>
